package com.zjw.zy.coreui.view

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.content.Context
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import com.zjw.zy.base.BaseViewGroup
import com.zjw.zy.R
import com.zjw.zy.utils.AnimationUtils

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/12/26 15:59
 */
class WechatInputView : BaseViewGroup {

    private var mListener: Listener? = null

    private val mEditText by lazy { findViewById<EditText>(R.id.wechat_input_edit) }
    private val mPickup by lazy { findViewById<ImageView>(R.id.wechat_input_pickup) }
    private val mFunc by lazy { findViewById<ImageView>(R.id.wechat_input_func) }
    private val mFuncGroup by lazy { findViewById<ViewGroup>(R.id.wechat_input_func_group) }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    override fun onLayoutId(): Int {
        return R.layout.wechat_input_view_layout
    }

    override fun onBindListener() {

        mFunc.setOnClickListener {
            mListener?.onFuncClick()
            toggleFuncGroup()
        }

        mPickup.setOnClickListener {
            mListener?.onPickUpClick()
        }

        mEditText.setOnKeyListener { v, keyCode, event ->
            if (event?.action == KeyEvent.ACTION_UP) {
                when (keyCode) {
                    KeyEvent.KEYCODE_ENTER -> {
                        val message = mEditText.text.toString().replace("\n", "")
                        mEditText.setText("")
                        if (message.isEmpty()) {
                            return@setOnKeyListener true
                        }

                        mListener?.onCompleteText(message)

                        return@setOnKeyListener true
                    }
                }
            }
            false
        }

    }

    private fun toggleFuncGroup() {
        if (mFuncGroup.visibility != View.VISIBLE) {
            mFuncGroup.visibility = View.VISIBLE
        } else {
            mFuncGroup.visibility = View.GONE
        }
    }

    override fun onBindValue() {}
    override fun onFindView(rootView: View) {}


    fun bindListener(listener: Listener) {
        mListener = listener
    }

    interface Listener {
        fun onCompleteText(str: String)
        fun onPickUpClick()
        fun onFuncClick()
    }
}